//
//  ActivityBubbleViewController.m
//  KidReading
//
//  Created by YangShuai on 16/10/13.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ActivityBubbleViewController.h"
#import "ConstDefine.h"
@interface ActivityBubbleViewController ()
{
    TLBubbleView* bubbleView;
    ShowDirection _showDirection;
    ArrowPosition _arrowPosition;
}
@end

@implementation ActivityBubbleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmpty)];
    [self.view addGestureRecognizer:tap];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

- (void)tapEmpty
{
    //
    CGPoint pt = bubbleView.center;
    if (_showDirection == ShowDirection_Left) {
        pt.x -= bubbleView.frame.size.width*0.5;
    }else if (_showDirection == ShowDirection_Right){
        pt.x += bubbleView.frame.size.width*0.5;
    }
    [UIView animateWithDuration:0.3f animations:^{
        bubbleView.center = pt;
        bubbleView.transform = CGAffineTransformMakeScale(0.000001, 1);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self willMoveToParentViewController:nil];
        [self removeFromParentViewController];
        if (_dismissBlock) _dismissBlock();
    }];
    
}

- (void)unShowAndRemove{
    [self tapEmpty];
}

//纯文本气泡
- (void)showBubbleViewToActivityVC:(UIViewController*)vc Introduce:(NSString*)introduceText{
    //
    CGFloat radius = 12;
    CGFloat arrowHeight = 12;
    CGFloat textLabelWidth = 200;
    CGFloat textFont = 20;
    CGFloat textTop = 40;
    CGFloat textLeft = 20;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        radius = 6;
        arrowHeight = 8;
        textLabelWidth = self.view.width / 3.2;
        textFont = 14;
        textTop = 30;
        textLeft = 12;
    }
    //
    [vc addChildViewController:self];
    [vc.view addSubview:self.view];
    [self didMoveToParentViewController:vc];
    //创建label
    UILabel* textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textLabelWidth, 1000)];
    textLabel.font = [UIFont systemFontOfSize:textFont];
    textLabel.text = introduceText;
    [textLabel resizeToFit];
    CGPoint pt = self.view.center;
    if (_followView) {
        pt = [_followView.superview convertPoint:_followView.center toView:self.view];
        if (pt.x >= self.view.center.x) {
            _showDirection = ShowDirection_Right;
            pt.x -= _followView.frame.size.width*0.6;
        }else{
            _showDirection = ShowDirection_Left;
            pt.x += _followView.frame.size.width*0.6;
        }
        if (pt.y < self.view.height / 3) {
            _arrowPosition = ArrowPosition_Top;
        }else if (pt.y > self.view.height / 3 * 2){
            _arrowPosition = ArrowPosition_Bottom;
        }else{
            _arrowPosition = ArrowPosition_Center;
        }
    }
    if (!bubbleView) {
        bubbleView = [TLBubbleView TLBubbleViewWithSize:CGSizeMake(textLabel.frame.size.width + textTop, textLabel.frame.size.height + textLeft)];
        [self.view addSubview:bubbleView];
        [bubbleView addSubview:textLabel];
        if (_showDirection == ShowDirection_Right) {
            textLabel.center = CGPointMake(bubbleView.center.x - arrowHeight / 2, bubbleView.center.y);
        }else if (_showDirection == ShowDirection_Left){
            textLabel.center = CGPointMake(bubbleView.center.x + arrowHeight / 2, bubbleView.center.y);
        }
        
        
    }
    [bubbleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(undo)]];
    //
    bubbleView.radius = radius;
    bubbleView.borderWidth = 2;
    bubbleView.arrowHeight = arrowHeight;
    bubbleView.arrowAngle = M_PI/6;
    if (_showDirection == ShowDirection_Left) {
        bubbleView.arrowDirectorType = TLBubbleArrowType_Left;
    }else if (_showDirection == ShowDirection_Right){
        bubbleView.arrowDirectorType = TLBubbleArrowType_Right;
    }
    bubbleView.arrowLineRatio = 0.5;
    if (_arrowPosition == ArrowPosition_Top) {
        bubbleView.arrowLineRatio = 0.1;
    }else if (_arrowPosition == ArrowPosition_Bottom) {
        bubbleView.arrowLineRatio = 0.9;
    }
    bubbleView.backgroundColor = [UIColor whiteColor];
    bubbleView.pt_Arrow = pt;
    bubbleView.borderColor = [UIColor colorWithHexString:@"4A90E2"];
    [bubbleView displayMask];
    bubbleView.alpha = 1;
    //
    pt = bubbleView.center;
    if (_showDirection == ShowDirection_Left) {
        bubbleView.center = CGPointMake(pt.x - bubbleView.frame.size.width*0.5, pt.y);
    }else if (_showDirection == ShowDirection_Right){
        bubbleView.center = CGPointMake(pt.x + bubbleView.frame.size.width*0.5, pt.y);
    }
    bubbleView.transform = CGAffineTransformMakeScale(0.000001, 1);
    [UIView animateWithDuration:0.2f animations:^{
        bubbleView.center = pt;
        bubbleView.transform = CGAffineTransformMakeScale(1, 1);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)undo{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
